#ifndef _TCPLIB_EVENTHUB_HPP_
#define _TCPLIB_EVENTHUB_HPP_
#include "Common.hpp"
#include <ext/hash_map>

namespace tcplib { 

class EventHub : public Noncopyable
{
    typedef void (*IOCB_FUNC)(void *, int);
    typedef void (*TIMERCB_FUNC)(void *, uint32_t);

    struct IOCallback
    {
        IOCallback()
            : count(0), readcb(NULL),writecb(NULL),errorcb(NULL)
        {}

        int count;
        IOCB_FUNC readcb;
        void *readarg;
        IOCB_FUNC writecb;
        void *writearg;
        IOCB_FUNC errorcb;
        void *errorarg;
    };

    struct TimerCallback
    {
        TIMERCB_FUNC cb;
        void *arg;
    };



    __gnu_cxx::hash_map<int, IOCallback> _io_cbmap;
    __gnu_cxx::hash_map<uint32_t, TimerCallback> _timer_cbmap;
    int _epoll_fd;
    void *_timer_pool;
    bool _is_running;

public:
    EventHub();
    ~EventHub();

    enum {
        READABLE = 0x01,
        WRITABLE = 0x02,
        ERROR = 0x04,
        MAXEVENT = 0x8,
    };

    int add_io_event(int fd, int event, void (*handler)(void *, int), void *obj);
    int remove_io_event(int fd, int event);


    uint32_t open_timer();
    int set_timer(uint32_t tfd, uint32_t expire_ms, void (*handler)(void *, uint32_t), void *obj);
    int cancel_timer(uint32_t tfd);
    int close_timer(uint32_t tfd);

    void run();
    void stop();
};



};

#endif

